/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package huffman;


public class HuffmanTree implements Comparable
{

BinaryTree<Leaf> root; // root of tree
int totalWeight; // weight of tree
public HuffmanTree(Leaf e){
    root=new BinaryTree(e);
    totalWeight=e.frequency;
}
// post: construct a Leaf with associated character
public HuffmanTree(HuffmanTree left, HuffmanTree right){
    this.totalWeight=left.totalWeight+right.totalWeight;
    root=new BinaryTree(null, left.root, right.root);
}
// pre: left and right non-null
// post: merge two trees together and merge their weights
 
public int compareTo(HuffmanTree other){
    if(this.totalWeight<other.totalWeight){
        return -1;
    }
    else{
        if(this.totalWeight==other.totalWeight){
            return 0;
        }else
        {
            return 1;
        }
    }
 
}
// pre: other is non-null
// post: return integer reflecting relation between values
   
public boolean equals(HuffmanTree that){
    if(this==that){
        return true;
    }else{
        return false;
    }
}
// post: return true if this and that are same tree instance
public void print(){
    print(root, "");
}
// post: print out strings associated with characters in tree
protected void print(BinaryTree r, String datos){
    if(r.getLeft()==null){
       
        Leaf e = (Leaf)r.getValue();
        if(e!=null)
        System.out.println("Caracter: " + e.ch + " Codigo: " + datos + " Frecuencia: " + e.frequency );

    }else{
           
            print(r.getLeft(), datos+"0");
            print(r.getRight(), datos+"1");
    }
}
    @Override
public int compareTo(Object o) {
            HuffmanTree a = (HuffmanTree)o;
            return this.totalWeight - a.totalWeight;
}
// post: print out strings associated with chars in tree r,
// prefixed by representation
}